package com.it.system.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.base.exception.BusinessException;
import com.it.system.domain.Logininfo;
import com.it.system.domain.Menu;
import com.it.system.dto.LoginDTO;
import com.it.system.jwt.JwtUtil;
import com.it.system.jwt.Payload;
import com.it.system.mapper.LogininfoMapper;
import com.it.system.query.LogininfoQuery;
import com.it.system.service.ILogininfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class LogininfoServiceImpl implements ILogininfoService {

    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    public List<Logininfo> findAll() {
        return logininfoMapper.findAll();
    }

    @Override
    public Logininfo findOne(Long id) {
        return logininfoMapper.findOne(id);
    }

    @Override
    public void delete(Long id) {
        logininfoMapper.deleteById(id);
    }

    @Override
    public void add(Logininfo logininfo) {
        logininfoMapper.add(logininfo);
    }

    @Override
    public void update(Logininfo logininfo) {
        logininfoMapper.update(logininfo);
    }

    @Override
    public void patchDelete(Long[] ids) {
        logininfoMapper.deletePatchByIds(ids);
    }

    @Override
    public PageInfo<Logininfo> queryPage(LogininfoQuery logininfoQuery) {
        PageHelper.startPage(logininfoQuery.getCurrentPage(), logininfoQuery.getPageSize());
        List<Logininfo> logininfos = logininfoMapper.queryPage(logininfoQuery);
        return new PageInfo<Logininfo>(logininfos);
    }

    @Override
    public String login(LoginDTO loginDTO) {
        Logininfo logininfo=logininfoMapper.findLoginInfo(loginDTO);
        if (logininfo == null) {
            throw new BusinessException("账户不存在");
        }
        String password = SecureUtil.md5(loginDTO.getPassword()+logininfo.getSalt());
        if (!password.equals(logininfo.getPassword())) {
            throw new BusinessException("密码错误!");
        }
        Payload payload=new Payload();
        logininfo.setSalt(null);//数据脱敏
        logininfo.setPassword(null);//数据脱敏
        payload.setLogininfo(logininfo);
        if (loginDTO.getType() == 0) {
            List<String> permissions=logininfoMapper.findPs(logininfo.getId());
            payload.setPermissions(permissions);
            List<Menu> menus=getmenuTree(logininfo.getId());
            payload.setMenus(menus);
        }
        String jwt = JwtUtil.generateToken(payload);
        return jwt;
    }

    private List<Menu> getmenuTree(Long id) {
        List<Menu> list=logininfoMapper.findMs(id);
        Map<Long, Menu> menuMap =
                list.stream().collect(
                        Collectors.toMap(Menu::getId, (d) -> d)
                );
        //3.定义返回的typeTree集合
        List<Menu> typeTree = new ArrayList<Menu>();
        //4.找到顶层类型，没有上级类型就是顶级类型
        for (Menu menu : list) {
            //equals自己的id直接跳过，Long缓存问题，用equals
            //if (menu.getId().equals(id)) continue;
            if (menu.getParentId() == null) {
                typeTree.add(menu);
            } else {//非顶级类型加入到父级类型下
                //找到父类型
                Menu parentMenu = menuMap.get(menu.getParentId());
                //添加自己到父类型的子类型集合中
                parentMenu.getChildren().add(menu);
            }
        }
        return typeTree;
    }
}
